/**
 * huize Service Inc
 * All Rights Reserved @2018
 */
package com.huize.zhike.framework.common.util;

import com.huize.ladon.apm.common.util.HzJsonUtils;
import com.huize.ladon.apm.common.enums.DateTypeEnum;
import com.huize.zhike.framework.common.enumeration.StatTypeEnum;
import com.huize.zhike.framework.common.param.ChannelRangeParam;
import com.huize.zhike.framework.common.param.DateRangeParam;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 描述:
 *
 * @author tianyuliang
 * @version $Id: DateTrendUtils.java, v0.1 2022/4/12
 */
public class DateParamUtils {

    /**
     * 构建保费查询时间范围 当日/当月/当年(yyyyMMdd)
     */
    public static DateRangeParam buildDateRangeParam(Date accessDate, StatTypeEnum statTypeEnum, DateTypeEnum typeEnum) {
        Date currentDate = accessDate;
        String end = DatetimeUtils.formatExtDate(currentDate);
        String start = end;
        if (DateTypeEnum.isYear(typeEnum)) {
            start = DatetimeUtils.formatExtDate(DatetimesUtils.getDateYearOne(currentDate));
        } else if (DateTypeEnum.isMonth(typeEnum)) {
            start = DatetimeUtils.formatExtDate(DatetimesUtils.getDateMonthOne(currentDate));
        } else if (DateTypeEnum.isDay(typeEnum)) {
            start = end;
        }
        DateRangeParam param = new DateRangeParam(start, end, statTypeEnum);
        return param;
    }

    /**
     * 构建保费查询时间范围 当日/当月/当年(yyyy-MM-dd)
     */
    public static DateRangeParam buildSpecialDateRangeParam(Date accessDate, DateTypeEnum typeEnum, StatTypeEnum statTypeEnum) {
        Date currentDate = accessDate;
        String end = DatetimeUtils.formatDate(currentDate);
        String start = end;
        if (DateTypeEnum.isYear(typeEnum)) {
            start = DatetimeUtils.formatDate(DatetimesUtils.getDateYearOne(currentDate));
        } else if (DateTypeEnum.isMonth(typeEnum)) {
            start = DatetimeUtils.formatDate(DatetimesUtils.getDateMonthOne(currentDate));
        } else if (DateTypeEnum.isDay(typeEnum)) {
            start = end;
        }
        DateRangeParam param = new DateRangeParam(start, end, statTypeEnum);
        return param;
    }


    /**
     * 计算当月 时间刻度线
     */
    public static String buildMonthTimeRatio(Date accessDate) {
        Date currentDate = accessDate;
        int totalDay = DatetimesUtils.getTotalDaysFromMonth(currentDate);
        int currentDay = DatetimesUtils.getCurrentDaysFromMonth(currentDate);
        Double divide = NumberUtils.percent(Double.valueOf(currentDay), Double.valueOf(totalDay));
        String result = NumberUtils.formatPercent(divide); // ; Math.round(divide) + "%";
        return result;
    }

    /**
     * 计算当年 时间刻度线
     */
    public static String buildYearTimeRatio(Date accessDate) {
        Date currentDate = accessDate;
        int totalDay = DatetimesUtils.getTotalDaysFromYear(currentDate);
        int currentDay = DatetimesUtils.getCurrentDaysFromYear(currentDate);
        Double divide = NumberUtils.percent(Double.valueOf(currentDay), Double.valueOf(totalDay));
        String result = NumberUtils.formatPercent(divide); // ; Math.round(divide) + "%";
        return result;
    }

    /**
     * 计算指定日期所在月份连续天数
     */
    public static List<String> buildCurrentMonthCountDay(Date accessDate) {
        Date firstDayDate = accessDate;                              // 当前日期
        return buildYearMonthCountDay(firstDayDate);
    }

    /**
     * 计算指定日期所在年份连续天数
     */
    public static List<String> buildCurrentYearCountDay(Date accessDate) {
        List<String> results = new ArrayList<>();
        Date firstDayDate = DatetimesUtils.getDateYearOne(accessDate);   // 当年1月1日
        int monthCount = 12;
        for (int i = 0; i < monthCount; i++) {
            Date nextMonthDate = DatetimeUtils.getNextMonth(firstDayDate, i);    // 下月1号
            List<String> days = buildYearMonthCountDay(nextMonthDate);
            results.addAll(days);
        }
        return results;
    }

    /**
     * 计算日期天数
     */
    private static List<String> buildYearMonthCountDay(Date firstDayDate) {
        Date nextMonthDate = DatetimeUtils.getNextMonth(firstDayDate, 1);    // 下月1号
        nextMonthDate = DatetimesUtils.getDateMonthOne(nextMonthDate);
        Date maxDate = DatetimeUtils.getPreDay(nextMonthDate, 1);           // 本月最大值
        Date minDate = DatetimesUtils.getDateMonthOne(maxDate);                 // 本月最小值

        Integer min = Integer.valueOf(DatetimeUtils.formatExtDate(minDate));
        Integer max = Integer.valueOf(DatetimeUtils.formatExtDate(maxDate));
        List<String> results = new ArrayList<>();
        for (Integer i = min; i <= max; i++) {
            results.add(i.toString());
        }
        return results;
    }

    /**
     * 构建对象 ChannelRangeParam
     */
    public static ChannelRangeParam buildChannelRangeParam(Date accessDate, StatTypeEnum statTypeEnum) {
        String end = DatetimeUtils.formatExtDate(accessDate);
        String start = DatetimeUtils.formatExtDate(DatetimesUtils.getDateMonthOne(accessDate));
        ChannelRangeParam param = new ChannelRangeParam(start, end, statTypeEnum);
        param.setSerialDates(buildMonthSerialDates(accessDate));
        return param;
    }

    /**
     * 构建连续月份
     */
    public static List<String> buildMonthSerialDates(Date accessDate) {
        List<String> serialDates = new ArrayList<>();
        Date firstDate = DatetimesUtils.getDateMonthOne(accessDate);
        String strFirstDate = DatetimeUtils.formatDate(firstDate);
        serialDates.add(strFirstDate);

        int count = 5;
        for (int i = 1; i <= count; i++) {
            Date otherDate = DatetimeUtils.getPreMonth(firstDate, i);
            String strOtherDate = DatetimeUtils.formatDate(otherDate);
            serialDates.add(strOtherDate);
        }
        return serialDates;
    }

    /**
     * 构建table表校验日期
     */
    public static DateRangeParam buildTableStatusParam(Date accessDate, StatTypeEnum statTypeEnum) {
        Date currentDate = accessDate;
        String end = DatetimeUtils.formatExtDate(DatetimeUtils.getPreDay(currentDate, 3));
        String start = end;
        DateRangeParam param = new DateRangeParam(start, end, statTypeEnum);
        return param;
    }

    public static void main(String[] args) {
        List<String> serialDates = buildMonthSerialDates(DatetimeUtils.parseDate("2021-04-03"));
        System.out.println(HzJsonUtils.beanToJson(serialDates));

        System.out.println("\n\n");
        buildCurrentMonthCountDay(DatetimeUtils.parseDate("2020-06-05"));

        System.out.println("\n\n");
        List<String> results = buildCurrentYearCountDay(DatetimeUtils.parseDate("2021-06-05"));
        results.forEach(System.out::println);
        System.out.println("size=" + results.size());
    }

}